
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * menuCadCliente.java
 *
 * Created on 12/08/2011, 08:11:01
 */
/**
 *
 * @author Anthony
 */
public class menuCadCliente extends javax.swing.JFrame {

    /** Creates new form menuCadCliente */
    public menuCadCliente() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoRA = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        campoCurso = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(102, 102, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("RA");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        campoRA.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                campoRAKeyPressed(evt);
            }
        });
        jPanel1.add(campoRA, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 10, 100, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Curso");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 50, -1, -1));
        jPanel1.add(campoCurso, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 50, 100, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Confirmar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 120, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 230, 150));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void campoRAKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoRAKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoRAKeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
            try{ 
            Connection conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/sisteca", "postgres", "postgres");
            String ra = campoRA.getText();
            String curso = campoCurso.getText();
            String sql = "select ra from cliente where ra = ?";
            
            PreparedStatement query = conexao.prepareStatement(sql);
            query.setString(1, ra);
            ResultSet rs = query.executeQuery();
            
            if(rs.next()){
                  JOptionPane.showMessageDialog(null, "Cliente já cadastrado!");
            }  
            else {
                if(campoRA.getText().equals("") || campoCurso.getText().equals("")){
                    JOptionPane.showMessageDialog(null, "campo(s) não preenchido!");
                }
                else{
                    sql = "insert into cliente(ra,curso,qtd) values(?,?,?)";
                    query = conexao.prepareStatement(sql);
                    query.setString(1, ra);
                    query.setString(2, curso);
                    query.setInt(3, 0);
                    query.execute();
                    JOptionPane.showMessageDialog(null, "Cliente cadastrado com sucesso!");
                    this.dispose();
                    
                  
                  
                }
                
            }
          query.close();
          rs.close();  
        } catch(Exception e){
            System.out.println("Deu erro...");
            e.printStackTrace();
        }
            
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new menuCadCliente().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoCurso;
    private javax.swing.JTextField campoRA;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
